package com.yc.jpyy.complaint.service.impl;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.commonbusiness.dao.CommonbusinessDao;
import com.yc.jpyy.complaint.dao.ComplaintInfoDao;
import com.yc.jpyy.complaint.service.ComplaintInfoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ComplaintInfoServiceImpl extends BaseService implements ComplaintInfoService {

    @Autowired
    private ComplaintInfoDao complaintInfoDao;
    
    @Autowired
    private CommonbusinessDao commonbusinessDao;

    @Override
    public int add(Map<String, Object> companyInfo) throws DataAccessException {
        return this.complaintInfoDao.add(companyInfo);
    }

    @Override
    public int update(Map<String, Object> companyInfo) throws DataAccessException {
        String companyreplytime = Optional.ofNullable(companyInfo.get("companyreplytime")).orElse("").toString();
        companyInfo.put("companyreplytime", companyreplytime.equals("")?null:companyreplytime);
        String companyhandletime = Optional.ofNullable(companyInfo.get("companyhandletime")).orElse("").toString();
        companyInfo.put("companyhandletime", companyhandletime.equals("")?null:companyhandletime);
        String complaintcenterreplytime = Optional.ofNullable(companyInfo.get("complaintcenterreplytime")).orElse("").toString();
        companyInfo.put("complaintcenterreplytime", complaintcenterreplytime.equals("")?null:complaintcenterreplytime);
        String distributetime = Optional.ofNullable(companyInfo.get("distributetime")).orElse("").toString();
        companyInfo.put("distributetime", distributetime.equals("")?null:distributetime);
        String revoketime = Optional.ofNullable(companyInfo.get("revoketime")).orElse("").toString();
        companyInfo.put("revoketime", revoketime.equals("")?null:revoketime);
        String recordtimepassenger = Optional.ofNullable(companyInfo.get("recordtimepassenger")).orElse("").toString();
        companyInfo.put("recordtimepassenger", recordtimepassenger.equals("")?null:recordtimepassenger);
        String recordtimedrive = Optional.ofNullable(companyInfo.get("recordtimedrive")).orElse("").toString();
        companyInfo.put("recordtimedrive", recordtimedrive.equals("")?null:recordtimedrive);
    	int i = complaintInfoDao.update(companyInfo);
    	if(i >0 ){
    		int j = complaintInfoDao.updates(companyInfo);
    		return j;
    	}
    	return i;
    }

    @Override
    public int del(Map<String, Object> companyInfo) throws DataAccessException {
        return this.complaintInfoDao.del(companyInfo);
    }

    @Override
    public int delforUpdate(Map<String, Object> companyInfo) throws DataAccessException {
        return this.complaintInfoDao.delforUpdate(companyInfo);
    }

    @Override
    public Map<String, Object> findByID(Map<String, Object> paramsMap) throws DataAccessException {
        return this.complaintInfoDao.findByID(paramsMap);
    }

    @Override
    public PageBean findPage(Map<String, Object> paramsMap) throws DataAccessException {
        int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
        int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
        paramsMap.put("pagestart", (pagenum-1) * pagesize);
        String isdistribute = Optional.ofNullable(paramsMap.get("isdistribute")).orElse("").toString();
        String complainantstateArray = Optional.ofNullable(paramsMap.get("complainantstate")).orElse("").toString();
        String becomplainantcompanyid = Optional.ofNullable(paramsMap.get("becomplainantcompanyid")).orElse("").toString();
        String becomplainantcompany = Optional.ofNullable(paramsMap.get("becomplainantcompany")).orElse("").toString();
        paramsMap.put("isdistribute",isdistribute.equals("")?"":isdistribute.split(",") );
        paramsMap.put("complainantstateArray",complainantstateArray.equals("")?"":complainantstateArray.split(","));
        paramsMap.put("becomplainantcompanyid",becomplainantcompanyid.equals("")?"":becomplainantcompanyid.split(",") );
        paramsMap.put("becomplainantcompany",becomplainantcompany.equals("")?"":becomplainantcompany.split(","));
        //HandlerDataSource.setDataSource("bizDataSource");
        List<Map<String, Object>> list = this.complaintInfoDao.findPage(paramsMap);
        PageBean pageBean = new PageBean();
        pageBean.setList(list);
        // 每页几条数据，默认20条
        pageBean.setPageSize(pagesize);
        // 得到数据库中的条数
        pageBean.setAllRow(this.findCount(paramsMap));
        // 当前第几页，默认第一页
        pageBean.setCurrentPage(pagenum);
        // 得到一共几页
        pageBean.setTotalPage(pageBean.getTotalPage());
        // 模仿百度分页，只显示10条数据
        pageBean.paginationTool(pagenum, pageBean.getTotalPage());
        return pageBean;
    }
    
    @Override
    public PageBean findPagePrint(Map<String, Object> paramsMap) throws DataAccessException {
        int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
        int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
        paramsMap.put("pagestart", (pagenum-1) * pagesize);
        String isdistribute = Optional.ofNullable(paramsMap.get("isdistribute")).orElse("").toString();
        String complainantstateArray = Optional.ofNullable(paramsMap.get("complainantstate")).orElse("").toString();
        String becomplainantcompanyid = Optional.ofNullable(paramsMap.get("becomplainantcompanyid")).orElse("").toString();
        String becomplainantcompany = Optional.ofNullable(paramsMap.get("becomplainantcompany")).orElse("").toString();
        paramsMap.put("isdistribute",isdistribute.equals("")?"":isdistribute.split(",") );
        paramsMap.put("complainantstateArray",complainantstateArray.equals("")?"":complainantstateArray.split(","));
        paramsMap.put("becomplainantcompanyid",becomplainantcompanyid.equals("")?"":becomplainantcompanyid.split(",") );
        paramsMap.put("becomplainantcompany",becomplainantcompany.equals("")?"":becomplainantcompany.split(","));
        //HandlerDataSource.setDataSource("bizDataSource");
        List<Map<String, Object>> list = this.complaintInfoDao.findPagePrint(paramsMap);
        PageBean pageBean = new PageBean();
        pageBean.setList(list);
        // 每页几条数据，默认20条
        pageBean.setPageSize(pagesize);
        // 得到数据库中的条数
        pageBean.setAllRow(this.findPrintCount(paramsMap));
        // 当前第几页，默认第一页
        pageBean.setCurrentPage(pagenum);
        // 得到一共几页
        pageBean.setTotalPage(pageBean.getTotalPage());
        // 模仿百度分页，只显示10条数据
        pageBean.paginationTool(pagenum, pageBean.getTotalPage());
        return pageBean;
    }

    @Override
    public int findCount(Map<String, Object> paramsMap) throws DataAccessException {
        return this.complaintInfoDao.findCount(paramsMap);
    }
    
    public int findPrintCount(Map<String, Object> paramsMap) throws DataAccessException {
        return this.complaintInfoDao.findPrintCount(paramsMap);
    }

    @Override
    public List getVehicleAll(Map<String,Object> paramsMap) {
        return this.complaintInfoDao.getVehicleAll(paramsMap);
    }

    @Override
    public List getVehicleByVehicleNo(Map<String, Object> paramsMap) {
        return this.complaintInfoDao.getVehicleByVehicleNo(paramsMap);
    }

    @Override
    public int complaintHandleByCentent(int id, String complaintCenterRequirements, String complaintCenterReply, Date date) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("complaintCenterRequirements",complaintCenterRequirements);
        map.put("complaintCenterReply",complaintCenterReply);
        map.put("date",date);
        this.complaintInfoDao.complaintHandleByCentent(map);
        return 1;
    }

    @Override
    public void complainDistribution(int id, int isDistribute, String distributeUser, Date distributeTime) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("isDistribute",isDistribute);
        map.put("distributeUser",distributeUser);
        map.put("distributeTime",distributeTime);
        this.complaintInfoDao.complainDistribution(map);
    }

    @Override
    public List getCompanyComplaint(Map<String, Object> paramsMap) {
        return this.complaintInfoDao.getCompanyComplaint(paramsMap);
    }

    @Override
    public int complaintHandleByCanpany(Map<String, Object> paramsMap) {    
        return this.complaintInfoDao.complaintHandleByCanpany(paramsMap);
    }

	@Override
	public int updateIsUrge(Map<String, Object> paramsMap) {
		return this.complaintInfoDao.updateIsUrge(paramsMap);
	}

    @Override
    public boolean isVehicleExists(Map<String,Object> paramsMap) {
        int count = this.complaintInfoDao.isVehicleExists(paramsMap);
        if(count > 0 ){
            return true;
        }
        return false;
    }

    @Override
    public PageBean getComplaintByVehicleNo(Map<String,Object> paramsMap) {
        int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
        int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
        paramsMap.put("pagestart", (pagenum-1) * pagesize);
        List list = this.complaintInfoDao.getCompanyComplaintByVehicleNo(paramsMap);
        PageBean pageBean = new PageBean();
        pageBean.setList(list);
        // 每页几条数据，默认20条
        pageBean.setPageSize(pagesize);
        // 得到数据库中的条数
        pageBean.setAllRow(this.complaintInfoDao.getVehicleByVehicleNoCount(paramsMap));
        // 当前第几页，默认第一页
        pageBean.setCurrentPage(pagenum);
        // 得到一共几页
        pageBean.setTotalPage(pageBean.getTotalPage());
        // 模仿百度分页，只显示10条数据
        pageBean.paginationTool(pagenum, pageBean.getTotalPage());
        return pageBean;
    }

    @Override
    public PageBean getVehiclesByIdNumber(Map<String, Object> paramsMap) {
        int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
        int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
        paramsMap.put("pagestart", (pagenum-1) * pagesize);
        List<Map<String,Object>> list = complaintInfoDao.getVehiclesByIdNumber(paramsMap);
        PageBean pageBean = new PageBean();
        pageBean.setList(list);
        // 每页几条数据，默认20条
        pageBean.setPageSize(pagesize);
        // 得到数据库中的条数
        pageBean.setAllRow(list.size()>0?(int)list.get(0).get("countsize"):0);
        // 当前第几页，默认第一页
        pageBean.setCurrentPage(pagenum);
        // 得到一共几页
        pageBean.setTotalPage(pageBean.getTotalPage());
        // 模仿百度分页，只显示10条数据
        pageBean.paginationTool(pagenum, pageBean.getTotalPage());
        return pageBean;
    }

	@Override
	public Map<String, Object> getVehicleOpenid(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		paramsMap.put("vehicleids", paramsMap.get("vehicleid").toString().split(","));
		List<Map<String,Object>> openList = complaintInfoDao.getVehicleOpenid(paramsMap);
		if(openList != null && openList.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", openList);
			resultMap.put("returnMsg", "查询成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> updateComplaintTransfer(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		paramsMap.put("ids", paramsMap.get("id").toString().split(","));
		int count = complaintInfoDao.updateComplaintTransfer(paramsMap);
		if(count > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "锁定成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "锁定失败");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> getComplaintInfoByPage(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		List<Map<String,Object>> dataList = complaintInfoDao.getComplaintInfoByPage(paramsMap);
		if(dataList != null && dataList.size() > 0){
			int dataCounts = (int) dataList.get(0).get("count");
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",dataList);
			resultMap.put("TotalCount",dataCounts);
			resultMap.put("TotalPage",dataCounts % pagesize >0 ? dataCounts/pagesize + 1 : dataCounts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "系统无数据");
			resultMap.put("ListData", dataList);
		}        	
		return resultMap;
	}
	
	@Override
	public Map<String, Object> checkIsBind(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		//根据openID查询记录，不存在，则未绑定
		Map<String,Object> map = complaintInfoDao.getComplainantPhone(paramsMap);
		if(map != null && map.size() > 0){
			//记录存在的情况下，查询电话号码，号码未存在，则未绑定
			String complainantPhone = Optional.ofNullable(map.get("lxrPhone")).orElse("").toString();
			if(complainantPhone == null || complainantPhone.isEmpty() || complainantPhone.equals("")){
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "该用户未绑定");
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("returnMsg", "该用户已绑定");
				resultMap.put("ListData", complainantPhone);
			}
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "该用户未绑定");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateComplaintAnnexByID(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int count = complaintInfoDao.updateComplaintAnnexByID(paramsMap);
		if(count > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "修改成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "修改失败");
		}
		return resultMap;
	}
	
	@Override
	public String getDbcode(Map<String, Object> paramsMap) throws Exception {
		String dbcode =  complaintInfoDao.getDbcode(paramsMap);
		if(dbcode == null || dbcode.equals("")){
			throw new Exception("车牌号错误");
		}
		return dbcode;
	}

	@Override
	public Map<String, Object> getDriverByVehicleNo(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		List<Map<String,Object>> map = complaintInfoDao.getDriverByVehicleNo(paramsMap);
		if(map != null && map.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", map);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}

	@Override
	public int addForWx(Map<String, Object> paramsMap) throws Exception {
		int retValue = 1;
		int num  = 0 ;
		num = complaintInfoDao.findwxUserCountToBlackDao(paramsMap);
	    if(num > 0){
		    throw new Exception("此用户为黑名单用户");
		}	  
	    num = complaintInfoDao.findwxUserComplaintByOneDao(paramsMap);
	    if(num > 0){
		    throw new Exception("同一用户同一车辆一天只能投诉一次");
		}
	    Map<String, Object> mapCompanyMsg = complaintInfoDao.findVehicleIdDao(paramsMap);
	    if(null != mapCompanyMsg) {
	    	paramsMap.put("becomplainantcompanyid", mapCompanyMsg.get("CompanyId"));
		    paramsMap.put("becomplainantcompany", mapCompanyMsg.get("CompanyName"));
		    paramsMap.put("annex", paramsMap.get("evidencepicture"));
	    }
		try {
			DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		    LocalDateTime date =LocalDateTime.now();
			paramsMap.put("admissibilitytime", date.format(f));
		    paramsMap.put("recordingstafftime", date.format(f));
		    paramsMap.put("datetimename",date.format(f));
		    paramsMap.put("daycount",7);
		    Map<String,Object> info = complaintInfoDao.selcetCalendarByDate(paramsMap);
		    paramsMap.put("transact", Optional.ofNullable(info.get("DateTimeName")).orElse("").toString());
			complaintInfoDao.add(paramsMap);
		} catch (Exception e) {
			retValue = 0;
		}
		return retValue;
	}


}
